<?
include("/var/www/html/new/conf/r.le.je");
$le=new go_sql();
$le->connect(); 

if (!class_exists("cpaint")){
  include("../cpaint/cpaint2.inc.php");
}
  $cp=new cpaint();

//截取
function mbsubstr($str, $width = 0, $end = '', $x3 = 0) {   
    global $CFG;  
    if ($width <= 0 || $width >= strlen($str)) {   
        return $str;   
    }   
    $arr = str_split($str);   
    $len = count($arr);   
    $w = 0;   
    $width *= 10;   
  
    $x1 = 11;
    $x2 = 16;   
    $x3 = $x3===0 ? ( $CFG['cf3']  > 0 ? $CFG['cf3']*10 : $x3 = 21 ) : $x3*10;   
    $x4 = $x3;   
  
    for ($i = 0; $i < $len; $i++) {   
        if ($w >= $width) {   
            $e = $end;   
            break;   
        }   
        $c = ord($arr[$i]);   
        if ($c <= 127) {   
            $w += $x1;   
        }   
        elseif ($c >= 192 && $c <= 223) { 
            $w += $x2;   
            $i += 1;   
        }   
        elseif ($c >= 224 && $c <= 239) { 
            $w += $x3;   
            $i += 2;   
        }   
        elseif ($c >= 240 && $c <= 247) {
            $w += $x4;   
            $i += 3;   
        }   
    }   
  
    return implode('', array_slice($arr, 0, $i) ). $e;   
} 

function p_match($matchstr){
	$guestexp = '\xA1\xA1|^Guest|^\xD3\xCE\xBF\xCD|\xB9\x43\xAB\xC8';
	if (preg_match("/^\s*$|^c:\\con\\con$|[%,\*\"\'\s\t\<\>\&\\\\\/\\\$]|$guestexp/is", $matchstr)) {
		return true;
	}else{
		return false;
	}
}

function ulogin($uname,$pwd,$yzm,$po=0) {
  $node=$GLOBALS['cp']->add_node("uloginn");
  $ultext=""; 
  if (p_match($uname)) {
	  $ultext.="0 | 错误 | 用户名不能为空或含有非法字符";
	  $node->set_data($ultext);
      return;	
  }
  
  
  
   $str="select did,username,password,qx_lvl,if_fb,getPY(x_m,1) as xm1,getPY(x_m,2) as xm2 from uu_admin where username=\"".$uname."\"";
    $GLOBALS['le']->query($str);
    $GLOBALS['le']->fetch_assoc();
	$did=$GLOBALS['le']->f("did");
	$dpass=$GLOBALS['le']->f("password");
	$qx_lvl=$GLOBALS['le']->f("qx_lvl");
	$if_fb=$GLOBALS['le']->f("if_fb");
	$lastdate=$GLOBALS['le']->f("lastdate");
	$ddate=$GLOBALS['le']->f("ddate");
	$xm1=$GLOBALS['le']->f("xm1");
	$xm2=$GLOBALS['le']->f("xm2");
	
$str_s="select chktime from uu_admin_login where duser='".$uname."' order by did desc limit 1";
	$GLOBALS['le']->query($str_s);
    $GLOBALS['le']->fetch_assoc();
    $chktime=$GLOBALS['le']->f("chktime");
	$chktime=$chktime?$chktime:0;
	$extime=$_SERVER['REQUEST_TIME']-$chktime;

	if ($extime<=5) {
	$ultext.="0 | 错误发生 | 此账户已在其他地方登录！";
	  $node->set_data($ultext);
      return;	
	}

	if (!$did) {
	$ultext.="0 | 错误 | 用户名不存在";
	  $node->set_data($ultext);
      return;	
	}
	
	if ($if_fb==1) {
	$ultext.="0 | 错误 | 对不起您的账号被禁止登录！";
	  $node->set_data($ultext);
      return;	
	}
	
	if ($qx_lvl<100) {
	$ultext.="0 | 错误 | 您的权限等级不够！";
	  $node->set_data($ultext);
      return;	
	}	
	
	if (md5($pwd.$xm1.$xm2)!=$dpass){
	$ultext.="0 | 错误 | 密码错误";	

    }elseif ($yzm!="" && !strtolower($_SESSION["checkWord"])) {
	$ultext.="0 | 错误 | 验证码过期失效";

	}elseif ($yzm=="" || strtolower($yzm)!=strtolower($_SESSION["checkWord"])) {
	$ultext.="0 | 错误 | 验证码错误";
	}else{
	$ultext.="1 | |";
	$_SESSION["uuadmin"]=$uname;
	$_SESSION["uudid"]=$did;
	$_SESSION["uuaddr"]=$_SERVER['REMOTE_ADDR'];
	$_SESSION["uutime"]=$_SERVER['REQUEST_TIME'];

	$str="insert into uu_admin_login set duser='$uname',dip='".$_SERVER['REMOTE_ADDR']."',logintime='".$_SESSION["uutime"]."',chktime='$chktime',loginmark='1'";
	$GLOBALS['le']->query($str);

	$str="update uu_admin set lastdate=now(), lastip='".$_SERVER['REMOTE_ADDR']."' where username='$uname'";
	$GLOBALS['le']->query($str);
	}
	  $node->set_data($ultext);
      return;	  
}

//实时（监控）消息
function Chk_AdmLogin_ajax($n){
	$node=$GLOBALS['cp']->add_node("Chk_AdmLogin_ajax_n");	
	$dtext="";
	
	//$dtext="0|后台系统临时重启下，请暂时退出，过会儿再登录！";
	//$node->set_data($dtext);
    //return;
	
    $str_s="select qx_lvl from uu_admin where username='".$_SESSION["uuadmin"]."'";
	$GLOBALS['le']->query($str_s);
    $GLOBALS['le']->fetch_assoc();
	$qx_lvl=$GLOBALS['le']->f("qx_lvl");


if ($qx_lvl>=100){
$str_s="select dip,logintime,chktime,loginmark from uu_admin_login where duser='".$_SESSION["uuadmin"]."' order by did desc limit 1";
	$GLOBALS['le']->query($str_s);
    $GLOBALS['le']->fetch_assoc();
	$dip=$GLOBALS['le']->f("dip");
    $logintime=$GLOBALS['le']->f("logintime");
	$chktime=$GLOBALS['le']->f("chktime");
	$loginmark=$GLOBALS['le']->f("loginmark");
	$chktime=$chktime?$chktime:0;
	$extime=$_SERVER['REQUEST_TIME']-$chktime;
	$limittime=60*60*24*7;
	if ($extime>$limittime){
	$chktime=$_SERVER['REQUEST_TIME']-$limittime;
	}
	
if ($_SESSION["uutime"]!=$logintime || $_SESSION["uuaddr"]!=$dip ) {
$dtext="1|此账号在其他地方登录了";
}elseif($loginmark!='1' && $extime>600){
//$dtext="3|".$_SERVER['REQUEST_TIME']."#".$chktime."#".$extime;
$dtext="3|太长时间没动作断线了";
}else{
$str_s="select did from uu_admin_login where duser='".$_SESSION["uuadmin"]."' order by did desc limit 1";
$GLOBALS['le']->query($str_s);
$GLOBALS['le']->fetch_assoc();
$s_did=$GLOBALS['le']->f("did");
	
$str_s="update uu_admin_login set chktime='".$_SERVER['REQUEST_TIME']."',loginmark='0' where did=$s_did";
$GLOBALS['le']->query($str_s);

$a_l_user=array();
$a_user=array();
$a_dtxt=array();

$str="select duser,unix_timestamp()-logintime as utime from uu_admin_login where unix_timestamp()-chktime<8 and duser='".$_SESSION["uuadmin"]."' order by did desc limit 1";
$GLOBALS['le']->query($str);
$row=$GLOBALS['le']->fetch_assoc();
$a_l_user[]=$row;

$str="select duser,unix_timestamp()-logintime as utime from uu_admin_login where unix_timestamp()-chktime<8 and duser<>'".$_SESSION["uuadmin"]."' order by did desc limit 13";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
  $a_l_user[]=$row;
}


foreach ($a_l_user as $v) {
$str="select did,x_m,qx_lvl,b_z from uu_admin where username='".$v[duser]."' and qx_lvl>99 limit 1";
$GLOBALS['le']->query($str);
if ($a_user=$GLOBALS['le']->fetch_assoc()) {
$a_user['b_z']=mbsubstr($a_user['b_z'],17);
$dtxt1=implode(',',$a_user);
$a_dtxt[]=$dtxt1.",".mod_utime($v[utime]);
}
}
$dtext=implode('#',$a_dtxt);
$dtext="2|".$dtext;
}
if ($n==2) {
$mtxt="|";
//1,2返回登录,退出信息
$ain=array();
$str="select a.x_m from uu_admin_login b left join uu_admin a on b.duser=a.username where unix_timestamp()-b.logintime<5 and b.duser<>'".$_SESSION["uuadmin"]."' order by b.did desc";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
  $ain[]=$row[x_m];
}
$mtxt.=implode('#',$ain);

$mtxt.="|";
$aout=array();
$str="select a.x_m from uu_admin_login b left join uu_admin a on b.duser=a.username where unix_timestamp()-b.chktime>15 and unix_timestamp()-b.chktime<20 and b.duser<>'".$_SESSION["uuadmin"]."' order by b.did desc";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
  $aout[]=$row[x_m];
}
$mtxt.=implode('#',$aout);

//3,聊天记录
$achat=array();
$str="select a.x_m,b.dchat,from_unixtime(b.dtime) as dt from uu_admin_chat b left join uu_admin a on b.duser=a.username where b.dtime>=".$chktime." and b.duser<>'".$_SESSION["uuadmin"]."' order by b.id asc";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
  $achat[]=$row;
}
$mtxt.="|";
if ($ca1=count($achat)){
foreach ($achat as $k=>$v){
$mtxt.=$v[x_m]."HH".$v[dchat]."HH".$v[dt];
if ($k<($ca1-1)) $mtxt.="#";
}//endforeach
}//endif

//监控警告

$str="select wtime,(unix_timestamp()-unix_timestamp(wtime)) as ktime,status from uu_watchdog where id=1";
$GLOBALS['le']->query($str);
$GLOBALS['le']->fetch_assoc();
$wtime=$GLOBALS['le']->f('wtime');
$ktime=$GLOBALS['le']->f('ktime');
$dss=$GLOBALS['le']->f('status');
if ($ktime>9){
$xtime=$ktime-9;
if ($ca1) $mtxt.="#";
$mtxt.="系统HH<img src='images/warning1.png' class='imgie6' align='absmiddle' />&nbsp;<font color=red>警告！监控系统发现数据已中断超过&nbsp;<span class='pinkbig'>$xtime</span>&nbsp;秒，请及时处理！</font>HH".$wtime;
$str="update uu_watchdog set status=2 where id=1";
$GLOBALS['le']->query($str);
}elseif ($ktime<=9 && $dss==2){
if ($ca1) $mtxt.="#";
$alert_D="系统HH<img src='/images/1.png' class='imgie6' align='absmiddle' />&nbsp;<font color=green>监控系统发现数据中断已经恢复！</font>";
$str="insert uu_admin_rec set daction=\"".$alert_D."\",addtime=now()";
$GLOBALS['le']->query($str);
$str="update uu_watchdog set status=0 where id=1";
$GLOBALS['le']->query($str);
}

//4,订单信息
$aorder=array();
$str="select * from uu_order_trace where unix_timestamp(rectime)>=".$chktime." order by rectime asc limit 100";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
  $aorder[]=$row;
}

$mtxt.="|";
$alertword="";
if ($ca2=count($aorder)){
$a_order_status=array("正常可使用","已被使用","已过期","被取消","已被替代");
foreach ($aorder as $k=>$v){
//取得从属
if ($v[paystatus]==3) {
$str="select dname from uu_distributor where id='".$v[fromt]."'";	
$GLOBALS['le']->query($str);
$GLOBALS['le']->fetch_assoc();
$ordertxt=$GLOBALS['le']->f('dname');
}else{
$str="select notes from dataex where account='".$v[fromt]."'";	
$GLOBALS['le']->query($str);
$GLOBALS['le']->fetch_assoc();
$ordertxt=$GLOBALS['le']->f('notes');	
}

$ordertxt.="的";

//if ($v[daction]!="del") $orderlink="<a href='jq/adminSceneryOrderDetail.html?ordernum=".(int)$v[ordernum]."' target='_self'>".$v[ordernum]."</a>";
if ($v[daction]!="del") $orderlink="<a href='javascript:void[0]' onmouseup=go_URL('jq/adminSceneryOrderDetail.html?ordernum=".(int)$v[ordernum]."') target='_self'>".$v[ordernum]."</a>";

else $orderlink=$v[ordernum];
$ordertxt.=($v[daction]!="new")?"":"新";
$ordertxt.="订单".$orderlink;

switch ($v[daction]) {
case "new": $ordertxt.="创建了";break;	
case "up": $ordertxt.=$a_order_status[$v[status]];break;
case "del": $ordertxt.="被删除了";break;
default:break;
}
$mtxt.=$ordertxt."。&nbsp;<font color=gray>[".$v[rectime]."]</font>";
//$mtxt.=$v[ordernum]."HH".$v[status]."HH".$vtime."HH".$v[action]."HH".$v[paystatus]."HH".$v[fromt]."HH".$alertword;
if ($k<($ca2-1)) $mtxt.="#";
}//endforeach

//驴妈妈订单监控
/*foreach ($aorder as $key=>$val) {
	if ($val[fromt]=='lvmama'){
	$str="select pname,money,dtime from uu_partner_fund where partner='lvmama' and status=1 and dtime is not NULL";
    $GLOBALS['le']->query($str);
	if ($GLOBALS['le']->fetch_assoc()){
	$money=$GLOBALS['le']->f('money');
	$dtime=$GLOBALS['le']->f('dtime');
	$pname=$GLOBALS['le']->f('pname');
	
	$str="select sum(tprice*tnum) as tmoney,sum(tnum) as cnum from uu_jq_order where paystatus=1 and status=1 and dtime>'$dtime' and fromt='lvmama'";
    $GLOBALS['le']->query($str);
	$GLOBALS['le']->fetch_assoc();
	$tmoney=$GLOBALS['le']->f('tmoney');
	$cnum=$GLOBALS['le']->f('cnum');
	
	$str="select count(*) as ctotal from uu_jq_order where paystatus=1 and status=1 and ordertime>'$dtime' and fromt='lvmama'";
    $GLOBALS['le']->query($str);
	$GLOBALS['le']->fetch_assoc();
	$ctotal=$GLOBALS['le']->f('ctotal');
	
	$wdtime=substr($dtime,0,10);
	if ($tmoney>$money){
	$alertword.="<img src='images/warning1.png' class='imgie6' style='vertical-align:middle' align='absmiddle' /><font color=red>警告！".$pname."预付金额已使用&nbsp;<font color=yellow><b>".$tmoney."</b></font>&nbsp;元。已超出设置&nbsp;<font color=yellow><b>".($tmoney-$money)."</b></font>&nbsp;元！请马上处理！</font>";
	}else{
	$alertword.="从<font color=gray>".$wdtime."</font>始到现在,<font color=white>".$pname."</font>有效订单已累积&nbsp;<font color=white>".$ctotal."</font>&nbsp;单,总共票数：<font color=white>".$cnum."</font>&nbsp;张。金额累计：<font color=yellow><b>".$tmoney."</b></font>&nbsp;元。";		
	}
	}
	if ($alertword) break;
	}
}*/
}//endif

//5,操作记录
$arec1=array("admin|","kefu|","biz|","licence|","member|","page|","comment|","|1","|2","|3","|4","|5","moneyH","fromH","changeH","salerH","16u|","opinion|");
$arec2=array("管理员","客服","商户","商户证件","会员","权限页面","一条评论","创建了","资料更新了","被删除了","新增了","评论被删除了","资金账户","从","变成了","分销商","先行网","一条意见");
$recs=array();
$arec=array();
$str="select * from uu_admin_rec where unix_timestamp(addtime)>=".$chktime;
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
  $recs[]=$row;
}
foreach ($recs as $k=>$v){
$arec[]=str_replace($arec1, $arec2, $v);
}

$mtxt.="|";
if ($ca1=count($arec)){
foreach ($arec as $k=>$v){
$mtxt.=$v[daction]."HH".strrel($v[addtime]);
if ($k<($ca1-1)) $mtxt.="#";
}//endforeach
}//endif

//退款
$a_bm=array();

$str="select * from uu_refundlog where refund_status='0'";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
  $a_bm[]=$row;
}
if ($cabm=count($a_bm)) {
foreach ($a_bm as $v){
  $alertword.="<img src='images/warning1.png' class='imgie6' align='absmiddle' />";
  $alertword.="&nbsp;";
  $alertword.="<font color=pink>会员编号";
  $alertword.=$v['memid'];
  $alertword.="申请退款<font color=white><b>";
  $alertword.=$v['refund_money']/100;
  $alertword.="</b>元</font>未处理";
  $alertword.="申请时间：<font color=white>[".$v['apply_time']."]</font>";
  $alertword.="</font>";
  $alertword.="<br />";
}	
}

//分销申请
$a_fx_apply=array();

$str="select * from uu_dist_apply where status=0";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
  $a_fx_apply[]=$row;
}
if ($afxapply=count($a_fx_apply)) {
foreach ($a_fx_apply as $v){
  $alertword.="<img src='images/warning1.png' class='imgie6' align='absmiddle' />";
  $alertword.="&nbsp;";
  $alertword.="<font color=#00FFCC>新增分销申请";
  $alertword.="<font color=white>".$v['name']."</font>";
  $alertword.="&nbsp;机构：<font color=#009966>".$v['comp_name'];
  $alertword.="</font>未处理";
  $alertword.="申请时间：[".$v['creattime']."]";
  $alertword.="</font>";
  $alertword.="<br />";
}	
}
//$alertword="这行测试可以忽略嗯嗯";
$mtxt.="|".$alertword;
$dtext.=$mtxt;

}
}else{
$dtext="0|权限不够强制退出";
}

	$node->set_data($dtext);
    return;
}

function mod_utime($t){
$nH=floor($t/(60*60));
$nM=floor($t/60) % 60;
$nS=floor($t % 60);	

$txt="";
$txt.=($nH>=1)?$nH."小时":"";
$txt.=($nM>=1)?$nM."分钟":"";
$txt.=($nS>=1)?$nS."秒":"";

return $txt;	
}

function strrel($s){
$a1=array('#','|');
$a2=array('','');	
return str_replace($a1,$a2,$s);
}

function s_motion($s){
  for ($i=27;$i>0;$i--){
  $ks="/".$i;
   if (is_int(strpos($s,$ks))){
    return str_replace($ks,"<img src='images/motions/".$i.".gif' />",$s);
	break;
   }
  }
  return $s;
}

function send_chat_ajax($word){
	$node=$GLOBALS['cp']->add_node("send_chat_ajax_n");	
	$dtext="";

if (!$_SESSION["uuadmin"]){
	$dtext="0|用户已经失效，请重新登录！";
	$node->set_data($dtext);
    return;		
}
$t1=$_SERVER['REQUEST_TIME'];
if (is_int($word) && strlen($word)==10){
$str="select id from uu_jq_order where ordernum='".$word."'";
$GLOBALS['le']->query($str);
$GLOBALS['le']->fetch_assoc();
$id=$GLOBALS['le']->f("id");
if ($id) {
$word="<span onmouseup=go_URL('jq/adminSceneryOrderDetail.html?ordernum=$word')>".$word."</span>";	
}
}else{
$word=strrel($word);
$word=htmlspecialchars($word);
$word=s_motion($word);
}
$str="insert uu_admin_chat set duser='".$_SESSION["uuadmin"]."',dchat=\"".$word."\",dtime='$t1'";
$GLOBALS['le']->query($str);

$str="select x_m from uu_admin where username='".$_SESSION["uuadmin"]."'";
$GLOBALS['le']->query($str);
$GLOBALS['le']->fetch_assoc();
$x_m=$GLOBALS['le']->f('x_m');

$dtext="1|".$x_m."|".$word;
$node->set_data($dtext);
return;	
}

//后台检校验证码
function chk_pzm_ajax($n){
	$node=$GLOBALS['cp']->add_node("chk_pzm_ajax_n");
	$dtext="";
	
if (!class_exists("pgconn")){
include("/var/www/html/open/conf/pg.le.je");
$lee = new pgconn();
$lee->getConn();
}

include_once("/var/www/html/open/fun.php");
/*if ($n==1) $n="99999";
$nstr=" where id='$n'";

$pstr="select id,vouch_code from order_info".$nstr;
$lee->pgquery($pstr);
$at=$lee->pgFetchAssoc();

$table=($at[id]>1205000000)?"uu_jq_order":"uu_ss_order";

$str="select l.title ltitle,t.title ttitle,ordernum,certnum,o.salerid from $table o left join uu_land l on o.lid=l.id left join uu_jq_ticket t on o.tid=t.id where ordernum='$n' limit 1";
$GLOBALS['le']->query($str);
$as=$GLOBALS['le']->fetch_assoc();

$tcode=$at[vouch_code];
$scode=$as[salerid].hashstr($as[certnum],$as[salerid]);
$flag=($scode==$tcode)?0:1;
$pname=$as[ltitle].$as[ttitle];

//下个
$nstr=" where id>'$n' order by id asc limit 1";
$pstr="select id from order_info".$nstr;
$lee->pgquery($pstr);
$atn=$lee->pgFetchAssoc();

$pzm_node_l =$node->add_node('pzm_chk');
$pzm_node_l->set_attribute('flag', $flag);
$pzm_node_l->set_attribute('pname', $pname);
$pzm_node_l->set_attribute('scode', $scode);
$pzm_node_l->set_attribute('tcode', $tcode);
$pzm_node_l->set_attribute('nid', $atn[id]);
*/
$an1=array();
$an2=array();
$an3=array();

//终端
$pstr="select id,vouch_code from order_info order by upd_time desc";
$lee->pgquery($pstr);
while($row=$lee->pgFetchAssoc()){
$an1[$row[id]]=$row;
}

//服务端
$str="select l.title ltitle,t.title ttitle,ordernum,certnum,o.salerid from uu_jq_order o left join uu_land l on o.lid=l.id left join uu_jq_ticket t on o.tid=t.id";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
$an2[$row[ordernum]]=$row;
}

$str="select l.title ltitle,t.title ttitle,ordernum,certnum,s.salerid from uu_ss_order s left join uu_land l on s.lid=l.id left join uu_jq_ticket t on s.tid=t.id";
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
$an3[$row[ordernum]]=$row;
}

$am=$an2+$an3;


foreach	($an1 as $k=>$v) {
	//商品名
	$p_name=$am[$k]['ltitle'].$am[$k]['ttitle'];
	
    //比较凭证码
	//获取S端凭证码
	$s_pzm=hashstr($am[$k]['certnum'],$am[$k]['salerid']);
	$s_code=$am[$k]['salerid'].$s_pzm;
	
	//获取T端凭证码
	$t_code=$v['vouch_code'];
	
	$flag=($s_code==$t_code)?0:1;
	
	if ($flag){
	$pzm_node_l =$node->add_node('pzm_chk');
	$pzm_node_l->set_attribute('flag', $flag);
	$pzm_node_l->set_attribute('pname', $p_name);
	$pzm_node_l->set_attribute('id', $k);
	$pzm_node_l->set_attribute('scode', $s_code);
	$pzm_node_l->set_attribute('tcode', $t_code);
	}
}//循环

}

function u_example(){
	$node=$GLOBALS['cp']->add_node("u_example_n");	
	$dtext="";

	$node->set_data($dtext);
    return;	
}

$cp->register("ulogin");
$cp->register("Chk_AdmLogin_ajax");
$cp->register("send_chat_ajax");
$cp->register("chk_pzm_ajax");

$cp->start();
$cp->return_data();
?>